package com.showdoc.controller;

import com.showdoc.mapper.ItemMapper;
import com.showdoc.mapper.UserMapper;
import com.showdoc.pojo.Item;
import com.showdoc.pojo.User;
import com.showdoc.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;

/**
 * (Item)表控制层
 *
 * @author makejava
 * @since 2023-04-10 10:01:26
 */
@CrossOrigin
@RestController
@RequestMapping("item")
@EnableAutoConfiguration
public class ItemController {
    /**
     * 服务对象
     */
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询所有项目
     *
     */

    @GetMapping("/all")
    public List<Item> queryAll() {
        return itemMapper.queryAll();
    }

    /**
     * 查询用户的项目
     */

    @GetMapping("/myList")
    public R queryByUid(HttpServletRequest request){
        Long uid  =(Long) request.getSession().getAttribute("user");
        List<Item> items = itemMapper.selectByUid(uid);

        return R.success(items);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param
     * @return 单条数据
     */
    @PostMapping("/info")
    public R<Item> queryById(@RequestBody Item temp) {
        Long itemId = temp.getItemId();
        Item item = itemMapper.queryById(itemId);
        return R.success(item);
    }

    /**
     * 创建项目
     *
     * @param item 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public R addItem(@RequestBody Item item, HttpServletRequest request) {
        //获取当前用户
        Long id = (Long)request.getSession().getAttribute("user");
        User user = userMapper.selectByUid(id);
        //设置或更新项目字段
        Timestamp time = new Timestamp(System.currentTimeMillis());
        if(itemMapper.queryByName(item.getItemName()) != null){
            return R.error("项目已存在，请重新输入....");
        }else{
//       更新逻辑
//       temMapper.insertOrUpdateBatch(user.getUid());
//       temMapper.updateItem(user.getUsername());
//       tem.setLastUpdateTime(time.getTime());
         item.setUid(user.getUid());
         item.setUsername(user.getUsername());
         item.setAddtime(time.getTime());
         item.setLastUpdateTime(time.getTime());
        }
        //判断是否保存成功并返回信息
        boolean save = itemMapper.save(item);
        if (save) {
            return R.success(item);
        } else {
            return R.error("新建失败");
        }
    }
    //修改项目
    @PostMapping("/update")
    public R updateItem(@RequestBody Item item)
    {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        item.setLastUpdateTime(timestamp.getTime());

        int update = itemMapper.update(item);
        if(update == 1){
            return R.success("修改成功");
        }
        return R.error("修改失败请重试...");
    }

    /**
     * 编辑数据
     *
     * @param item 实体
     * @return 编辑结果
     */
//    @PutMapping
//    public ResponseEntity<Item> edit(Item item) {
//        return ResponseEntity.ok(this.itemMapper.update(item));
//    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
//    @DeleteMapping
//    public ResponseEntity<Boolean> deleteById(Long id) {
//        return ResponseEntity.ok(this.itemMapper.deleteById(id));
//    }

}

